package com.cakemallproject.controller;

import com.cakemallproject.pojo.Result;
import com.cakemallproject.pojo.mapperObj.*;
import com.cakemallproject.pojo.plusObj.OrderItemsDetails;
import com.cakemallproject.pojo.plusObj.OrdersDetail;
import com.cakemallproject.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TypeService typeService;

    final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @GetMapping
    public Result list() {
        log.info("获取订单列表");
        List<Orders> list = ordersService.list();
        return Result.success(list);
    }

    @PostMapping("/{cartIds}")
    public Result order(@PathVariable List<Integer> cartIds, @RequestBody Orders orders) {
        log.info("下单 order: {},ids: {}", orders,cartIds);

        //补充格式化后的时间，添加订单，通过时间获取订单
        orders.setCreateTime(LocalDateTime.parse(LocalDateTime.now().format(dateTimeFormatter),dateTimeFormatter));
        ordersService.insert(orders);
//        orders = ordersService.getByCreateTimeAndUid(orders.getCreateTime(),orders.getUid());
        log.info("orders: {}",orders);

        //获取购物车信息
        List<Cart> cartList = cartService.getCartListByCid(cartIds);

        //补充订单详情
        for (Cart cart : cartList) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOid(orders.getOid());
            orderItem.setGid(cart.getGid());
            orderItem.setNum(cart.getNum());
            orderItem.setPrice(goodsService.getById(cart.getGid()).getPrice());
            orderItemService.insert(orderItem);
        }
        //删除购物车对应信息
        cartService.delete(cartIds);


        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        ordersService.delete(ids);
        return Result.success();
    }

    @PutMapping("/complete/{ids}")
    public Result complete(@PathVariable List<Integer> ids) {
        log.info("订单 {} 完成", ids);
        ordersService.complete(ids);
        return Result.success();
    }
    @PutMapping("/deliver/{ids}")
    public Result deliver(@PathVariable List<Integer> ids) {
        log.info("订单 {} 发货", ids);
        ordersService.deliver(ids);
        return Result.success();
    }

    @GetMapping("/{uid}")
    public Result getOrderDetailByUid(@PathVariable Integer uid) {
        log.info("获取用户 {} 订单详情", uid);

        List<Orders> orders = ordersService.getByUid(uid);

        List<OrdersDetail> ordersDetails = new ArrayList<>();
        for (Orders order : orders) {
            List<OrderItem> orderItems = orderItemService.getByOid(order.getOid());

            List<OrderItemsDetails> orderItemsDetails = new ArrayList<>();
            for (OrderItem orderItem : orderItems) {
                Goods goods = goodsService.getById(orderItem.getGid());
                Type type = typeService.getById(goods.getTid());
                orderItemsDetails.add(new OrderItemsDetails(orderItem,goods,type));
            }
            ordersDetails.add(new OrdersDetail(order,orderItemsDetails));
        }

        return Result.success(ordersDetails);
    }
}
